#include "includes.h"
#include "structs.h"
#include "declares.h"


char *getbasename(char *path)
{
  char *startptr, *endptr;
  char *basename;

  if ((startptr = strrchr(path, '/')) == NULL) 
    startptr = path;
  else 
    startptr = startptr+1;

  basename = strdup(startptr);

  if ((endptr = strrchr(basename, '.')) != NULL) 
    *endptr = '\0';

  return basename;
}

void xfree(void **p)
{
        if(*p) {
                free(*p);
                *p=NULL;
        }
}

void print_pair(FILE *s, void *ptr)
{
        struct pair_t *p=ptr;
        fprintf(s,"my_pair(vid=%d,dist=%d)\n", 
                        p->vid,
                        p->dist);
}
void *xmalloc(int size)
{
        grape_errexit_if(size<=0, 1, "xmalloc: size <= 0\n");
        void *ptr = malloc(size);
        grape_errexit_if(!ptr, 1, "xmalloc: NO MEMORY\n");
        return ptr;
}

void print_vertex(FILE *s, struct vertex_t* v)
{
        fprintf(s,"vertex(vid=%d,ncon=%d,vsize=%d,attr=%p,fid=%d,locid=%d)fids=",
                        v->vid + 1,
                        v->ncon,
                        v->vsize,
                        v->attr,
                        v->fid,
                        v->locid);
        igraph_vector_int_fprint(v->fids, s);

}

void vertex_free(void *ptr)
{
        struct vertex_t *pv=ptr;
        igraph_vector_int_destroy(pv->fids);
        if(pv->ncon)
                free(pv->attr);
        free(pv);
}


void print_edge(FILE *s, struct edge_t *pedge)
{

#ifndef NDEBUG
        
        zlog_category_t *mylog = zlog_get_category("mylog");
        if(pedge->eid < 0) {
                zlog_error(mylog,"invalid eid %d", pedge->eid);
                abort();
        }
#endif
        fprintf(s, "edge(eid=%d,src=%d,dst=%d,attr=%d)\n",
                        pedge->eid + 1,
                        pedge->src + 1,
                        pedge->dst + 1,
                        pedge->attr);
}

char *va_format(char *fmt,...)
{

        char *cmd;
        size_t size = BUFSIZ;
        va_list ap;
        va_start(ap,fmt);
        while(1) {
                cmd = xmalloc(sizeof(char)*size);
                int sz=vsnprintf(cmd, size, fmt, ap);
                if(sz<0)
                        return NULL;
                if(size >= sz)         
                        break;

                size<<=1;
                cmd = realloc(cmd, size);
        }
        va_end(ap);
        return cmd;
}

int va_system(char *fmt,...)
{

        char *cmd;
        size_t size = BUFSIZ;
        va_list ap;
        va_start(ap,fmt);
        while(1) {
                cmd = xmalloc(sizeof(char)*size);
                int sz=vsnprintf(cmd, size, fmt, ap);
                if(sz<0)
                        return -1;
                if(size >= sz)         
                        break;

                size<<=1;
                cmd = realloc(cmd, size);
        }
        va_end(ap);
        int rs = system(cmd);
        free(cmd);
        return rs;
}  

struct edge_t *make_edge(int src, int dst, int attr,int eid)
{
        struct edge_t *pe=xmalloc(sizeof(struct edge_t));
        pe->src=src;
        pe->dst=dst;
        pe->attr=attr;
        pe->eid=eid;
        return pe;
}


int check_rfile(FILE *rfile, int npart)
{
        int vid,fid,err=0;
        zlog_category_t *mylog=zlog_get_category("mylog");

        while(!feof(rfile))
        {
                fscanf(rfile,"%d %d",&vid,&fid);
                if(fid >= npart || fid < 0) {
                        zlog_error(mylog,"invalid fid %d", fid);
                        ++err;
                }
        }
        if(err) {
                zlog_error(mylog,"rfile checked, %d errors detected", err);
                return 0;
        }
        zlog_info(mylog,"rfile checked, no errors detected");
        return 1;
}

